package com.znsb.msfq.album;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.znsb.msfq.BaseActivity;
import com.znsb.msfq.R;
import com.znsb.msfq.activity.AddShowBillActivity;
import com.znsb.msfq.utils.CacheActivityUtils;
import com.znsb.msfq.utils.IntentUtils;
import com.znsb.msfq.utils.ToastUtils;
import com.znsb.msfq.view.selectorphoto.Bimp;
import com.znsb.msfq.view.selectorphoto.ImageItem;
import com.znsb.msfq.view.selectorphoto.PublicWay;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 这个是显示一个文件夹里面的所有图片时的界面
 *
 * @author king
 * @version 2014年10月18日 下午11:49:10
 * @QQ:595163260
 */
public class ShowAllPhotoActivity extends BaseActivity {

    @Bind(R.id.bar_img_back)
    ImageView barImgBack;
    @Bind(R.id.bar_tv_title)
    TextView barTvTitle;
    @Bind(R.id.bar_tv_other)
    TextView barTvOther;
    @Bind(R.id.preview)
    TextView preview;
    @Bind(R.id.ok_buttonnum)
    TextView okButtonnum;
    @Bind(R.id.ok_buttonlayout)
    LinearLayout okButtonlayout;
    @Bind(R.id.bottom_layout)
    LinearLayout bottomLayout;
    @Bind(R.id.myGrid)
    GridView myGrid;
    @Bind(R.id.myText)
    TextView myText;
    private Intent intent;
    private AlbumGridViewAdapter gridImageAdapter;
    public static ArrayList<ImageItem> dataList = new ArrayList<ImageItem>();

    @Override
    protected int getlayoutView() {
        return R.layout.plugin_camera_album;
    }

    @Override
    protected void initView(View view) {
        PublicWay.activityList.add(this);
        this.intent = getIntent();
        String folderName = intent.getStringExtra("folderName");
        if (!folderName.equals("")) {
            if (folderName.length() > 8) {
                folderName = folderName.substring(0, 9) + "...";
            }
            barTvTitle.setText(folderName);
        }
        gridImageAdapter = new AlbumGridViewAdapter(this, dataList, Bimp.tempSelectBitmap);
        myGrid.setAdapter(gridImageAdapter);
        myGrid.setEmptyView(myText);
        okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
        barTvOther.setVisibility(View.VISIBLE);
        barTvOther.setText("取消");
        IntentFilter filter = new IntentFilter("data.broadcast.action");
        registerReceiver(broadcastReceiver, filter);
    }

    @Override
    protected void initData() {
        gridImageAdapter.setOnItemClickListener(new AlbumGridViewAdapter.OnItemClickListener() {
            public void onItemClick(final ToggleButton toggleButton, int position, boolean isChecked, Button button) {
                if (Bimp.tempSelectBitmap.size() >= PublicWay.num && isChecked) {
                    button.setVisibility(View.GONE);
                    toggleButton.setChecked(false);
                    ToastUtils.TextToast(ShowAllPhotoActivity.this, "超出可选图片张数", 2000);
                    return;
                }
                if (isChecked) {
                    button.setVisibility(View.VISIBLE);
                    Bimp.tempSelectBitmap.add(dataList.get(position));
                    okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
                } else {
                    button.setVisibility(View.GONE);
                    Bimp.tempSelectBitmap.remove(dataList.get(position));
                    okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
                }
                okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
            }
        });

    }

    @Override
    protected void clickListener(View view) {

    }

    BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            gridImageAdapter.notifyDataSetChanged();
        }
    };

    protected void onRestart() {
        okButtonnum.setText(Bimp.tempSelectBitmap.size() + "");
        super.onRestart();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bar_img_back, R.id.bar_tv_other, R.id.preview, R.id.ok_buttonlayout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bar_img_back:
                finish();
                break;
            case R.id.bar_tv_other:
                Bimp.tempSelectBitmap.clear();
                CacheActivityUtils.finishSingleActivityByClass(ImageFileActivity.class);
                CacheActivityUtils.finishSingleActivityByClass(AlbumActivity.class);
                finish();
                break;
            case R.id.preview:
                if (Bimp.tempSelectBitmap.size() > 0) {
                    Map<String, Object> map = new HashMap<>();
                    map.put("position", "1");
                    IntentUtils.startActivityAnimGeneral(this, AddShowBillActivity.class, map);
                } else {
                    ToastUtils.TextToast(this, "您未选择图片！", 2000);
                }
                break;
            case R.id.ok_buttonlayout:
                CacheActivityUtils.finishSingleActivityByClass(ImageFileActivity.class);
                CacheActivityUtils.finishSingleActivityByClass(AlbumActivity.class);
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(broadcastReceiver);
    }
}
